<?php

class Sistema_AcessopaginasController extends App_Controller_Action
{

    private $_table     = 'ACESSO_PAGINAS';
    private $_pk        = 'ACE_ID';
    private $_order_col = 'ACE_DATHOR';
    private $_order     = 'DESC';
    private $_sta_col   = '';
    private $obj        = '';
    private $form       = '';
    
    private $_PER_ID;
    private $_USU_ID;
    
    public function init()
    {
        if($this->getRequest()->getActionName() == 'edit') $this->form = new Sistema_Form_Acessopaginas();
        
        $auth = Zend_Auth::getInstance();
        $s_user = $auth->getStorage();
        $this->_PER_ID = $s_user->read()->PER_ID;
        $this->_USU_ID = $s_user->read()->USU_ID;
    }

    public function indexAction() {
        //$this->_helper->redirector('edit');
        
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('datetools','library');
        Zend_Loader::loadClass('elemform','library');
        Zend_Loader::loadClass('dominio','library');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/js/chosen/chosen.min.css'); 
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/chosen/chosen.jquery.min.js');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_acessopaginas.js');
        
        $where = 'm.'.$this->_pk . ' > 0';
        
        $dtfiltro = $this->_getParam('dtfiltro', '');
        $cbfiltro = $this->_getParam('cbfiltro', ''); 
        $tpfiltro = $this->_getParam('tpfiltro', '1'); 
        $mdfiltro = $this->_getParam('mdfiltro', ''); 
        $acfiltro = $this->_getParam('acfiltro', ''); 
        
        if($dtfiltro != '') $where .= ' and DATE(m.ACE_DATHOR) = "' .Datetools::dma_amd($dtfiltro).'"';
        if($cbfiltro > '') $where .= ' and m.USU_ID = ' . $cbfiltro;
        if($tpfiltro > '') $where .= ' and m.ACE_TPO = ' . $tpfiltro;
        if($mdfiltro > '') $where .= ' and m.ACE_CTL like "'.$mdfiltro.'"';
        if($acfiltro > '') $where .= ' and m.ACE_ACT like "'.$acfiltro.'"';
        
        $this->view->dtfiltro = $dtfiltro;
        $this->view->cbfiltro = $cbfiltro;
        $this->view->tpfiltro = $tpfiltro;
        $this->view->mdfiltro = $mdfiltro;
        $this->view->acfiltro = $acfiltro;
        
        if($this->_USU_ID > 1)
            $where .= ' AND m.USU_ID > 1';
        
        $query = 'SELECT SQL_CACHE m.*,u.USU_NOME FROM '.$this->_table.' m, USUARIOS u WHERE m.USU_ID = u.USU_ID AND '.$where.' ORDER BY m.'.$this->_order_col.' '.$this->_order;
        $result = $this->db->fetchAll($query);
        $this->view->result = $this->paginacao($result,20);
        
    }
    
    public function editAction() {
        
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('datetools','library');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/js/chosen/chosen.min.css'); 
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/chosen/chosen.jquery.min.js');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_acessopaginas.js');
        
        $this->view->form = $this->form;
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if($this->form->isValid($formData)) {
                    $id = (int) $this->form->getValue($this->_pk);
                    
                    $data = $this->form->getValues();
                    unset($data['submit']);
                    
                    if ($id == 0) $this->db->insert($this->_table,$data);
                    else $this->db->update($this->_table,$data, $this->_pk.' = ' . $id);
                    
                    $this->_helper->redirector('index');
                } else {
                    $this->form->populate($formData);
                }
            } catch(Exception $ex) {
                Zend_Loader::loadClass('errordb','library');
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                // echo $ex->getMessage();
                $this->form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $query = 'SELECT SQL_CACHE m.*,u.USU_NOME FROM '.$this->_table.' m, USUARIOS u WHERE m.USU_ID = u.USU_ID AND '.$this->_pk . ' = ' . $id;
                $data = $this->db->fetchRow($query);
                $data['ACE_DATHOR'] = Datetools::invertDH($data['ACE_DATHOR']);
                $this->form->populate($data);
            }
        }
    }

    public function deleteAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {   
            //$data = array($this->_sta_col => 0);
            //$ok = (bool) $this->obj->update($data, $this->_pk . ' = ' . $id);
            $this->db->delete($this->_table,$this->_pk . ' = ' . $id);
            
        }       
        echo json_encode(array('ok' => $ok));
    }

}